﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LogGenerator
{
    class Process_3_generator
    {
        string role1;
        string role2;
        string role3;
        

        List<string> role1Members;
        List<string> role2Members;
        List<string> role3Members;

        public Process_3_generator()
        {
            role1 = "Customer_Service_Role";
            role2 = "Airline_Agency_Role";
            role3 = "Offer_service_Role";
            
            role1Members = new List<string>();
            role1Members.Add("Originator_1_1");
            role1Members.Add("Originator_1_2");
            role1Members.Add("Originator_1_3");

            role2Members = new List<string>();
            role2Members.Add("Originator_2_1");
            role2Members.Add("Originator_2_2");
            role2Members.Add("Originator_2_3");

            role3Members = new List<string>();
            role3Members.Add("Originator_3_1");
            role3Members.Add("Originator_3_2");
            role3Members.Add("Originator_3_3");
        }

        private string GenerateTrace(string traceID, bool branch1)
        {
            Random randnum = new Random();
            StringBuilder output = new StringBuilder();
            output.Append("<ProcessInstance id=\"");
            output.Append(traceID);
            output.Append("\">");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Recieve_flight_request</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role1Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role1);
            output.Append("</Role>");
            output.Append("<Output>request</Output>");
            output.Append("</AuditTrailEntry>");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Flight_search_TA</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role2Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role2);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Recieve_flight_request\">request</Input>");
            output.Append("<Output>search_result_TA</Output>");
            output.Append("</AuditTrailEntry>");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Flight_search_BA</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role2Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role2);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Recieve_flight_request\">request</Input>");
            output.Append("<Output>search_result_BA</Output>");
            output.Append("</AuditTrailEntry>");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Make_flight_offers</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role3Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role3);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Flight_search_TA\">search_result_TA</Input>");
            output.Append("<Input Originator=\"Flight_search_BA\">search_result_BA</Input>");
            output.Append("<Output>offers</Output>");
            output.Append("</AuditTrailEntry>");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Send_flight_offers</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role3Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role3);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Make_flight_offers\">offers</Input>");
            output.Append("</AuditTrailEntry>");
            
            output.Append("</ProcessInstance>");

            return output.ToString();

        }

        private string GenerateProcess(string processID, int numOfTraces)
        {
            StringBuilder output = new StringBuilder();
            output.Append("<Process id=\"");
            output.Append(processID);
            output.Append("\">");
            Random randnum = new Random();
            bool branch = false;
            for (int idx = 0; idx < numOfTraces; idx++)
            {
                branch = randnum.Next(0, 2) != 0 ? true : false;
                output.Append(GenerateTrace("instance_" + idx, branch));
            }
            output.Append("</Process>");
            return output.ToString();
        }

        public void GenerateLog(string filename, int numOfTraces)
        {
            StringBuilder output = new StringBuilder();
            output.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
            output.Append("<WorkflowLog xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  xmlns=\"XSDMXML\" xsi:schemaLocation=\"XSDMXML NewLog.xsd\">");
            //output.Append("<Data>\r");
            //output.Append("</Data>\r");
            output.Append("<Source program=\"Process_3_generator\"/>");
            output.Append(GenerateProcess("Process_3", 15));
            output.Append("</WorkflowLog>");
            StreamWriter swriter = new StreamWriter(filename + ".xml");
            swriter.Write(output.ToString());
            swriter.Close();
        }
    }
}
